package nc.jmmc.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;

import nc.bs.dao.BaseDAO;
import nc.bs.dao.DAOException;
import nc.bs.framework.adaptor.IHttpServletAdaptor;
import nc.jdbc.framework.processor.ArrayListProcessor;
import nc.jdbc.framework.processor.ColumnProcessor;


public class MonitoringterminalServlet implements IHttpServletAdaptor {
/**
 * 监控终端号界面的servlet
 */
	BaseDAO dao=new BaseDAO();
	public void doAction(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");//编码格式设定
		request.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		String ncorder = request.getParameter("ncorder");//根据销售订单查询
		String scddhcx = request.getParameter("scddh");  //根据生产订单查询
		String jsongcd = request.getParameter("jsongcd");//将监控终端写入跟车单中
		if(ncorder!=null){
			try {
				String jkzd=orderByncorder(ncorder);//根据销售订单号查询监控终端号
				out.write(jkzd);
				
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(scddhcx!=null){
			try {
				String djkz=orderByscddh(scddhcx);//根据生产订单号查询监控终端号
				out.write(djkz);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(jsongcd!=null){
			JSONObject jso=null;
			try {
				jso = new JSONObject(jsongcd);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			String xsddh=jso.getString("xsddh");//销售订单号
			String scddh=jso.getString("scddh");//生产订单号
			String carnum=jso.getString("carnum");//车身号
			String vin=jso.getString("vin");//vin
			String jkzdxh=jso.getString("jkzdxh");//监控终端型号
			String jkzdbh=jso.getString("jkzdbh");//监控终端编号
			String userid = jso.getString("userid");//扫描人
			try {
				String res=insertgcd(xsddh,scddh,carnum,vin,jkzdxh,jkzdbh,userid);
				out.write(res);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		out.flush();
		out.close();
	}
	private String orderByncorder(String ncorder) {
		String sql=
			"select a.jkzdtype,c.scddh,c.zyx1,c.pch  from jmmc_car_info a\n" +
			"left join bd_invbasdoc v on a.invcode=v.invtype\n" + 
			"left join mm_mo c on v.pk_invbasdoc=c.wlbmid\n" + 
			"where c.xsddh='"+ncorder+"' and c.pk_corp='1021' and c.dr=0";
			ArrayList list =null;
			try {
				list = (ArrayList) dao.executeQuery(sql, new ArrayListProcessor());
			} catch (DAOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			JSONObject jso=new JSONObject();
			if(list.size()>0){
			for(int i=0;i<list.size();i++){
				Object[] o=(Object[]) list.get(i);
				jso.put("jkzdtype", o[0]==null||o[0].equals("")?"":o[0].toString());
				jso.put("scddh", o[1]==null||o[1].equals("")?"":o[1].toString());
				jso.put("zyx1", o[2]==null||o[2].equals("")?"":o[2].toString());
				jso.put("pch", o[3]==null||o[3].equals("")?"":o[3].toString());
			}
			}else{
				return "false";
			}
			return decodeUnicode(jso.toString());
		}
	private String insertgcd(String xsddh, String scddh, String carnum, String vin, String jkzdxh,String jkzdbh,String userid) throws Exception {
		String username = null;
		if(userid!=null&&!userid.equals("")){
        	String namesql = 
        		"select user_name from sm_user where cuserid='"+userid+"'";
        	username = (String) dao.executeQuery(namesql, new ColumnProcessor());
        }
		//		扫描人
        String smrsql = 
			"select def4 from jmmc_gcd  where vin='"+vin+"' and dr=0 and pk_corp='1021'";
        String smr = (String) dao.executeQuery(smrsql, new ColumnProcessor());
		String exzitsql = 
			"select terminalTBOX from jmmc_gcd  where vin='"+vin+"' and dr=0 and pk_corp='1021'";
        String exzit = (String) dao.executeQuery(exzitsql, new ColumnProcessor());
        String exzitsql1 = 
			"select terminalTBOX from jmmc_gcd  where terminalTBOX='"+jkzdbh+"' and dr=0 and pk_corp='1021'";
        String exzit1 = (String) dao.executeQuery(exzitsql1, new ColumnProcessor());
        if(exzit1==null||exzit1.equals("")){
        	if(exzit==null||exzit.equals("")){
		String sql="update jmmc_gcd set terminalTBOX='"+jkzdbh+"',terminalmodel= '"+jkzdxh+"',def4='"+smr+username+"&监控终端"+"' where vin='"+vin+"' and dr=0 and pk_corp='1021'";
		int i=dao.executeUpdate(sql);
		if(i==1){
			return "1";
		}else{
			return "0";
		}
        }else{
        	return "v";
        }
        }else{
        	return "d";
        }
		
	}
	private String orderByscddh(String scddh) throws Exception {
		String sql=
			"select a.jkzdtype,c.scddh,c.zyx1,c.pch,c.xsddh  from jmmc_car_info a\n" +
			"left join bd_invbasdoc v on a.invcode=v.invtype\n" + 
			"left join mm_mo c on v.pk_invbasdoc=c.wlbmid\n" + 
			"where c.scddh='"+scddh+"' and c.pk_corp='1021' and c.dr=0";
			ArrayList list =null;
			try {
				list = (ArrayList) dao.executeQuery(sql, new ArrayListProcessor());
			} catch (DAOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			JSONObject jso=new JSONObject();
			if(list.size()>0){
			for(int i=0;i<list.size();i++){
				Object[] o=(Object[]) list.get(i);
				jso.put("jkzdtype", o[0]==null||o[0].equals("")?"":o[0].toString());
				jso.put("scddh", o[1]==null||o[1].equals("")?"":o[1].toString());
				jso.put("zyx1", o[2]==null||o[2].equals("")?"":o[2].toString());
				jso.put("pch", o[3]==null||o[3].equals("")?"":o[3].toString());
				jso.put("xsddh", o[4]==null||o[4].equals("")?"":o[4].toString());
			}
			}else{
				return "false";
			}
			return decodeUnicode(jso.toString());
		}


	public static String decodeUnicode(String theString) {

		char aChar;

		int len = theString.length();

		StringBuffer outBuffer = new StringBuffer(len);

		for (int x = 0; x < len;) {

			aChar = theString.charAt(x++);

			if (aChar == '\\') {

				aChar = theString.charAt(x++);

				if (aChar == 'u') {

					// Read the xxxx    

					int value = 0;

					for (int i = 0; i < 4; i++) {

						aChar = theString.charAt(x++);

						switch (aChar) {

						case '0':

						case '1':

						case '2':

						case '3':

						case '4':

						case '5':

						case '6':
						case '7':
						case '8':
						case '9':
							value = (value << 4) + aChar - '0';
							break;
						case 'a':
						case 'b':
						case 'c':
						case 'd':
						case 'e':
						case 'f':
							value = (value << 4) + 10 + aChar - 'a';
							break;
						case 'A':
						case 'B':
						case 'C':
						case 'D':
						case 'E':
						case 'F':
							value = (value << 4) + 10 + aChar - 'A';
							break;
						default:
							throw new IllegalArgumentException(
									"Malformed   \\uxxxx   encoding.");
						}

					}
					outBuffer.append((char) value);
				} else {
					if (aChar == 't')
						aChar = '\t';
					else if (aChar == 'r')
						aChar = '\r';

					else if (aChar == 'n')

						aChar = '\n';

					else if (aChar == 'f')

						aChar = '\f';

					outBuffer.append(aChar);

				}

			} else

				outBuffer.append(aChar);

		}

		return outBuffer.toString();

	}

	

}